package com.paomi.client.activity.detail.presenter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.detail.adapter.MyListAdapter;
import com.paomi.client.activity.detail.adapter.PriceListAdapter;
import com.paomi.client.activity.detail.adapter.SeatListAdapter;
import com.paomi.client.activity.detail.adapter.TipListAdapter;
import com.paomi.client.activity.detail.model.Comment;
import com.paomi.client.activity.detail.model.CommentListBean;
import com.paomi.client.activity.detail.model.DetailTicketBean;
import com.paomi.client.activity.detail.model.PlanBean;
import com.paomi.client.activity.detail.model.TipCommentBean;
import com.paomi.client.activity.onlineSeat.OnlineSeatActivity;
import com.paomi.client.activity.person.model.CardJsonBean;
import com.paomi.client.activity.receive.adapter.CardReceiveListAdapter;
import com.paomi.client.activity.reply.model.ReplyContent;
import com.paomi.client.activity.detail.model.PraiseSeatBean;
import com.paomi.client.activity.pay.PayActivity;
import com.paomi.client.base.BaseLoadMorePresenter;
import com.paomi.client.base.BaseMvpLCEView;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.fragment.first.model.BannerBean;
import com.paomi.client.fragment.first.model.TicketJson;
import com.paomi.client.net.Api;
import com.paomi.client.net.BaseCallback;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.Datas;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.utils.UIUtils;
import com.paomi.client.utils.Util;
import com.paomi.client.view.LoadingDialog;
import com.paomi.client.view.calendarview.Calendar;
import com.paomi.client.view.calendarview.CalendarView;
import com.paomi.client.widget.RxToast;
import com.paomi.client.widget.StarBar;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * Created by pc on 2018/11/5
 * Describe:
 * com.paomi.client.activity.detail
 */
public class DetailTicketPresenter extends BaseLoadMorePresenter<BaseMvpLCEView> {
    List<ReplyContent> replyContents = new ArrayList<>();
    List<Comment> comments = new ArrayList<>();
    private String getId = null;

    /**
     * 访问演出详情接口
     *
     * @param id
     */
    public void setDetailData(String id) {
        getId = id;
        Api.getInstanceService(Constants.Ticket_URL).getDetailTicket("" + id).enqueue(new BaseCallback<DetailTicketBean>() {
            @Override
            public void onResponse(Call<DetailTicketBean> call, DetailTicketBean result) {
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        EventBus.getDefault().post(result);
                    }
                }, 100);
            }
        });
    }

    private int pageNum;
    private Activity mActivity;

    /**
     * 评论列表接口的调用
     *
     * @param page
     */
    public void setLoadData(Activity activity,int page) {
        mActivity = activity;
        pageNum = page;
        loadData(true);
    }

    @Override
    protected void loadData(int page, int skip, boolean pullToRefresh) {
        Map<String, String> maps = new HashMap<>();
        if (getId != null) {
            maps.put("type", "0");
            maps.put("typeId", "" + getId);//
            maps.put("replySize", "2");
            maps.put("pageNum", "" + pageNum);
            maps.put("pageSize", "" + Constants.PAGE_SIZE);
            maps.put("sort", "0");
            if (pullToRefresh) {
                Api.getInstanceService(Constants.Ticket_URL).getCommetList(maps).enqueue(new BaseCallback<CommentListBean>() {
                    @Override
                    public void onResponse(Call<CommentListBean> call, CommentListBean result) {
                        if (pageNum == 1) {
                            comments.clear();
                        }
                        if(comments.size()>0){
                            //判断是否有重复数据
                            boolean isData=false;
                            List<Comment>  comments1=result.getReturnData().getData();
                            for(Comment comment:comments1){
                                for(int i=0;i<comments.size();i++){
                                    if(comment.getId() == comments.get(i).getId()){
                                        isData=false;
                                        break;
                                    }else{
                                        isData=true;
                                    }
                                }
                            }
                            if(isData){
                                comments.addAll(result.getReturnData().getData());
                            }else{
                                ToastUtil.getToastEmail().ToastShow(mActivity, null, 1, R.layout.toast_view, mActivity.getResources().getString(R.string.data_null));
                            }
                        }else{
                            comments.addAll(result.getReturnData().getData());
                        }

                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                EventBus.getDefault().post(comments);
                            }
                        }, 1000);
                    }
                });
            }
        } else {
            getView().setData(comments, true);
        }

    }

    /**
     * 收藏接口的调用
     */
    public void addCollectionList(boolean isClick) {
        Map<String, String> stringMap = new HashMap<>();
        stringMap.put("typeId", "" + getId);
        stringMap.put("type", "0");
        Api.getInstanceService(Constants.Ticket_URL).getaddCollection(stringMap).enqueue(new BaseCallback<BaseJSON>() {
            @Override
            public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                if (isClick) {
                    result.returnMessage = "收藏成功";
                } else {
                    result.returnMessage = "取消收藏";
                }
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        EventBus.getDefault().post(result);
                    }
                }, 100);
            }
        });

    }

    /**
     * 点赞接口的调用
     */
    public void addLikeClick(boolean isClick, String id) {
        Api.getInstanceService(Constants.Ticket_URL).getaddLike(id).enqueue(new BaseCallback<BaseJSON>() {
            @Override
            public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                if (isClick) {
                    result.returnMessage = "点赞成功";
                } else {
                    result.returnMessage = "取消点赞";
                }
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        EventBus.getDefault().post(result);
                    }
                }, 100);
            }
        });

    }

    private RecyclerView list_time;//非在线选座的时间的列表显示
    private RecyclerView list_price;//价格的列表显示
    private RecyclerView list_get_time;//日历页面时间的列表显示
    private RecyclerView list_site;//非在线选座的座位显示
    private Button calendar_button;//选座购票按钮
    private List<PraiseSeatBean.PraiseSeatJson> timeData = new ArrayList<>();//非在线选座数据（价格选择）
    private List<PraiseSeatBean.PraiseSeatJson> seatData = new ArrayList<>();//非在线选座数据(座位选择)
    private List<PlanBean> timeList = new ArrayList<>();//日历页面时间的列表显示（场次信息数据-时间选择）
    private List<PlanBean> randomList = new ArrayList<>();//随机选座数据（场次信息数据-时间选择）
    private boolean isOrder = false;//是否选择票价
    private boolean isOrderGet = false;//是否选择票价
    private int post = 0;//时间选择的位置（演出场次和演出id的选择）
    private PraiseSeatBean.PraiseSeatJson praiseSeatJson;//选择座位票价信息
    private JSONObject jsonObject;//演出座次JSON串
    private List<String> keyList = new ArrayList<>();//保存的key
    private TextView num_ticket;//票数的显示
    private PlanBean planBean;//选中的场次信息
    private List<PraiseSeatBean.PraiseSeatJson> checkSeat = new ArrayList<>();//选择价格以及座位信息

    private Calendar textDate;//选中的日期(在线选座)
    private List<Calendar> calendarList = new ArrayList<>();//选中的日期索引指示的下标(在线选座)
    private int point = 0;//选择日期的位置，默认第一条(在线选座)
    private boolean isCanClick = false;//是否选择了日期(在线选座)
    private int minMonth=1;//月份最小值
    private int maxMonth=1;//月份最大值
    private int carMonth=0;

    /**
     * 显示日历选择售票日期的弹框（在线选座的跳转）
     * https://github.com/huanghaibin-dev/CalendarView
     *
     * @param activity
     * @param iconfont
     * @param type//是否在线选座 0 不选座 1选座
     */
    public void showTicketCalendar(Activity activity, Typeface iconfont, List<PlanBean> planBeans, DetailTicketBean.TicketBean ticketJson, int type, Button tvLogin) {
        timeList.clear();
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_ticket_calendar, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = activity.getResources().getDisplayMetrics().heightPixels / 5 * 3;
        window.setAttributes(lp);

        TextView left = view.findViewById(R.id.left_next);
        TextView right = view.findViewById(R.id.right_next);
        TextView dateTv = view.findViewById(R.id.date_tv);
        CalendarView calendarView = view.findViewById(R.id.calendarView);
        list_get_time = (RecyclerView) view.findViewById(R.id.list_get_time);
        calendar_button = (Button) view.findViewById(R.id.calendar_button);
        dateTv.setText(calendarView.getCurYear() + "年" + calendarView.getCurMonth() + "月");
        left.setTypeface(iconfont);
        right.setTypeface(iconfont);
        Map<String, Calendar> map = new HashMap<>();//组合选中有标记的日期
        calendarView.setOnMonthChangeListener(new CalendarView.OnMonthChangeListener() {
            @Override
            public void onMonthChange(int year, int month) {
                carMonth=month;
                dateTv.setText(year + "年" + month + "月");
            }
        });
        calendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                if (isClick) {
                    textDate = getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay());
                    if (map.containsValue(textDate)) {
                        list_get_time.setVisibility(View.VISIBLE);
                        timeList.clear();
                        planBeanGet = null;
//                        for (int i = 0; i < calendarList.size(); i++) {
//                            Calendar calendar1 = calendarList.get(i);
//                            if (calendar1.equals(textDate)) {
//                                point = i;
//                                isCanClick = true;
//                            }
//                        }
                        for (int i = 0; i < planBeans.size(); i++) {
                            PlanBean planBean = planBeans.get(i);
                            if (planBean.getState().equals("1")) {//判断是否有票 0 无票 1 有票
                                Date date = new Date(planBean.getShowDate() * 1000);

                                java.util.Calendar cal = java.util.Calendar.getInstance();
                                cal.setTime(date);
                                int year = cal.get(java.util.Calendar.YEAR);
                                int month = cal.get(java.util.Calendar.MONTH) + 1;
                                int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
                                if (textDate.equals(getSchemeCalendar(year, month, day))) {
                                    point=0;
                                    planBean.setType(0);
                                    timeList.add(planBean);
                                }
                            }
                        }
                        initListTime(activity);
                    }
                }
            }
        });
        //上一月
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(map.size()>0){
                    if(carMonth>minMonth){
                        calendarView.clearSingleSelect();
                        point=0;
                        calendarView.scrollToPre(false);
                        list_get_time.setVisibility(View.GONE);
                    }
//                    else{
//                        calendarView.scrollToCurrent();
//                    }
                }
            }
        });
        //下一月
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(map.size()>0){
                    if(carMonth < maxMonth){
                        calendarView.clearSingleSelect();
                        point=0;
                        list_get_time.setVisibility(View.GONE);
                        calendarView.scrollToNext(true);
                    }
                }
            }
        });
        calendarList.clear();
        //获取场次信息中演出时间的年月日
        for (int i = 0; i < planBeans.size(); i++) {
            PlanBean planBean = planBeans.get(i);
            //演出时间i
            if (planBean.getState().equals("1")) {//判断是否有票 0 无票 1 有票
                Log.e("演出时间", "" + Util.getTime((planBean.getShowDate() * 1000)));
                Date date = new Date(planBean.getShowDate() * 1000);

                java.util.Calendar cal = java.util.Calendar.getInstance();
                cal.setTime(date);
                int year = cal.get(java.util.Calendar.YEAR);
                int month = cal.get(java.util.Calendar.MONTH) + 1;
                int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
                map.put(getSchemeCalendar(year, month, day).toString(),
                        getSchemeCalendar(year, month, day));
                calendarList.add(getSchemeCalendar(year, month, day));
                //记录月份的最大值与最小值
                if(month>maxMonth){// 判断最大值
                    maxMonth=month;
                }
                if(month<minMonth) { // 判断最小值
                    minMonth = month;
                }
            }

        }
        calendarView.setSchemeDate(map);

        if (calendarList.size() > 0) {//有售票的日期
            //让日历滚动到当前日期
            calendarView.scrollToCalendar(calendarList.get(0).getYear(), calendarList.get(0).getMonth(), calendarList.get(0).getDay());
            randomList.clear();
            //选择第一个日期
            for (int i = 0; i < planBeans.size(); i++) {
                PlanBean planBean = planBeans.get(i);
                if (planBean.getState().equals("1")) {//判断是否有票 0 无票 1 有票
                    Date date = new Date(planBean.getShowDate() * 1000);

                    java.util.Calendar cal = java.util.Calendar.getInstance();
                    cal.setTime(date);
                    int year = cal.get(java.util.Calendar.YEAR);
                    int month = cal.get(java.util.Calendar.MONTH) + 1;
                    int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
                    if (calendarList.get(0).equals(getSchemeCalendar(year, month, day))) {
                        planBean.setType(0);
                        timeList.add(planBean);
                    }
                }
            }
            initListTime(activity);
        } else {//暂时没有票
            dialog.dismiss();
            tvLogin.setBackgroundColor(activity.getResources().getColor(R.color.color_88888));
            tvLogin.setClickable(false);
            RxToast.info(activity.getResources().getString(R.string.null_msg));
        }
        calendar_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (type == 1) {//在线选座
                    Intent intent = new Intent(activity, OnlineSeatActivity.class);
                    intent.putExtra("detail", (Serializable) ticketJson);//演出数据
                    if(timeList.size()==1){
                        intent.putExtra("plan", (Serializable) timeList.get(0));//选择场次数据
                    }else {
                        intent.putExtra("plan", (Serializable) timeList.get(point));//选择场次数据
                    }
                    activity.startActivity(intent);
                } else {//非在线选座
                    if (planBeanGet != null) {//选择同一天的一个时间段s
                        timeList.clear();
                        timeList.add(planBeanGet);
                    }
                    showTicketRandom(activity, iconfont, timeList, ticketJson);
                }

            }
        });

    }


    private Calendar getSchemeCalendar(int year, int month, int day) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        return calendar;
    }

    private DetailTicketBean.TicketBean ticketJson;

    /**
     * 直接选票和随机选座
     *
     * @param activity
     * @param iconfont
     * @param planBeans//场次信息
     * @param ticketJson//演出信息
     */
    public void showTicketRandom(Activity activity, Typeface iconfont, List<PlanBean> planBeans, DetailTicketBean.TicketBean ticketJson) {
        this.ticketJson = ticketJson;
        randomList.clear();
        checkSeat.clear();
        randomList.addAll(planBeans);
        jsonObject = new JSONObject();
        keyList = new ArrayList<>();
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_ticket_site, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = activity.getResources().getDisplayMetrics().heightPixels / 5 * 3;
        window.setAttributes(lp);
        TextView del_ticket = view.findViewById(R.id.del_ticket);
        del_ticket.setTypeface(iconfont);
        TextView add_ticket = view.findViewById(R.id.add_ticket);
        add_ticket.setTypeface(iconfont);
        num_ticket = view.findViewById(R.id.num_ticket);
        list_time = (RecyclerView) view.findViewById(R.id.list_time);
        list_price = (RecyclerView) view.findViewById(R.id.list_price);
        list_site = (RecyclerView) view.findViewById(R.id.list_site);
        calendar_button = (Button) view.findViewById(R.id.calendar_button);
        del_ticket.setOnClickListener(new View.OnClickListener() {//减少票数的按钮
            @Override
            public void onClick(View v) {
                if (num_ticket.getText().toString().equals("0")) {
                    jsonObject = new JSONObject();
                    return;
                }
                try {
                    if (checkSeat.size() > 0) {
                        if (checkSeat.contains(praiseSeatJson)) {
                            checkSeat.remove(praiseSeatJson);
                        }
                    }
                    if (keyList.size() > 0) {
                        for (int i = keyList.size() - 1; i == 0; i--) {
                            if (jsonObject.get(keyList.get(i)).toString().equals("1")) {
                                jsonObject.remove(keyList.get(i));
                            } else {
                                jsonObject.put(keyList.get(i), "" + (Integer.parseInt(jsonObject.get(keyList.get(i)).toString()) - 1));
                            }
                        }
                        if (!jsonObject.isNull("" + praiseSeatJson.getId())) {
                            if (jsonObject.get("" + praiseSeatJson.getId()).toString().equals("1")) {
                                jsonObject.remove("" + praiseSeatJson.getId());
                            } else {
                                jsonObject.put("" + praiseSeatJson.getId(), "" + (Integer.parseInt(jsonObject.get("" + praiseSeatJson.getId()).toString()) - 1));
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                num_ticket.setText("" + (Integer.parseInt(num_ticket.getText().toString()) - 1));
            }
        });
        add_ticket.setOnClickListener(new View.OnClickListener() {//增加票数按钮
            @Override
            public void onClick(View v) {
                if (!isOrderGet) {
                    RxToast.info(activity.getResources().getString(R.string.price_null));
                    return;
                }
                if (num_ticket.getText().toString().equals("" + planBean.getMaxNum())) {
                    RxToast.info(String.format(activity.getResources().getString(R.string.ticket_null), planBean.getMaxNum()));
                    calendar_button.getBackground().setAlpha(100);
                    return;
                }
                if (checkSeat.size() > 0) {
                    if (!checkSeat.contains(praiseSeatJson)) {
                        checkSeat.add(praiseSeatJson);
                    }
                } else {
                    checkSeat.add(praiseSeatJson);
                }
                try {
                    if (jsonObject.isNull("" + praiseSeatJson.getId())) {
                        jsonObject.put("" + praiseSeatJson.getId(), "1");
                        keyList.add("" + praiseSeatJson.getId());
                    } else {
                        jsonObject.put("" + praiseSeatJson.getId(), "" + (Integer.parseInt(num_ticket.getText().toString()) + 1));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                num_ticket.setText("" + (Integer.parseInt(num_ticket.getText().toString()) + 1));


            }
        });

        initListShow(activity);
        if (randomList.size() > 0) {//默认时间的第一条
            randomList.get(0).setType(1);
            randomList.get(0).getGoodsId();
            planBean = randomList.get(0);
            getRandomList("" + randomList.get(0).getGoodsId(), "" + randomList.get(0).getId(), activity);
        }
        calendar_button.setOnClickListener(new View.OnClickListener() {//确认订单
            @Override
            public void onClick(View v) {
                if (num_ticket.getText().toString().equals("0")) {
                    RxToast.info(activity.getResources().getString(R.string.num_null));
                    return;
                }
                if (isOrder) {
                    dialog.dismiss();
                    getIsOrderCan("" + randomList.get(post).getGoodsId(), "" + randomList.get(post).getId(), activity, jsonObject.toString());
                } else {
                    RxToast.info(activity.getResources().getString(R.string.price_null));
                }
            }
        });
    }

    /**
     * 非在线选座的请求
     *
     * @param goodId 演出ID
     * @param planId 演出场次ID
     */
    private void getRandomList(String goodId, String planId, Activity mcontext) {
        Map<String, String> stringMap = new HashMap<>();
        stringMap.put("goodsId", goodId);
        stringMap.put("goodsPlanId", planId);
        Api.getInstanceService(Constants.Ticket_URL).unOnlinSeat(stringMap).enqueue(new BaseCallback<PraiseSeatBean>() {
            @Override
            public void onResponse(Call<PraiseSeatBean> call, PraiseSeatBean result) {
                timeData.clear();
                timeData.addAll(result.getReturnData());
                initListPrice(mcontext);
            }
        });
    }

    /**
     * 校验能否下单
     *
     * @param goodId 演出ID
     * @param planId 演出场次ID
     *               goodsTicketJson	true	演出座次JSON串 {'112107' : '1', '112108':'1'} key 演出座次ID value 购买数量
     */
    private void getIsOrderCan(String goodId, String planId, Activity mcontext, String json) {
        LoadingDialog.get().showLoading();
        Map<String, String> stringMap = new HashMap<>();
        stringMap.put("goodsId", goodId);
        stringMap.put("goodsPlanId", planId);
        stringMap.put("goodsTicketJson", json);
        Log.e("checkSeat", "" + checkSeat.size());
        Api.getInstanceService(Constants.Pay_URL).isOrderCan(stringMap).enqueue(new BaseCallback<BannerBean>() {
            @Override
            public void onResponse(Call<BannerBean> call, BannerBean result) {
                LoadingDialog.get().hideLoading();
                Intent intent = new Intent(mcontext, PayActivity.class);
                intent.putExtra("detail", (Serializable) ticketJson);//演出数据
                intent.putExtra("plan", (Serializable) planBean);//选择场次数据
                intent.putExtra("checkSeat", (Serializable) checkSeat);//选择票价以及座位数据
                intent.putExtra("getData", Datas.toJsonString(result.getReturnData()));//选择场次数据
                mcontext.startActivity(intent);
            }
        });
    }

    PlanBean planBeanGet = null;//选择同一天的一个时间段

    /**
     * 时间(场次)选择
     *
     * @param mcontext
     */
    private void initListTime(Activity mcontext) {
        if(timeList.size()>0){
            timeList.get(0).setType(1);
        }
        //表格布局
        list_get_time.setLayoutManager(new GridLayoutManager(mcontext, 3));
//        list_time.setLayoutManager(getRecyclerViewManager( true,mcontext));
        MyListAdapter myListAdapter = new MyListAdapter(mcontext, timeList);
        list_get_time.setAdapter(myListAdapter);
        myListAdapter.OnItemClick(new MyListAdapter.OnItemClick() {
            @Override
            public void setOnItemClick(View view, int pos) {
                point=pos;
                planBeanGet = timeList.get(pos);
            }
        });
    }

    /**
     * 初始化时间选择(场次时间的数据)
     */
    private void initListShow(Activity mcontext) {
        //表格布局
        list_time.setLayoutManager(new GridLayoutManager(mcontext, 3));
//        list_time.setLayoutManager(getRecyclerViewManager( true,mcontext));
        MyListAdapter myListAdapter = new MyListAdapter(mcontext, randomList);
        list_time.setAdapter(myListAdapter);
        myListAdapter.OnItemClick(new MyListAdapter.OnItemClick() {
            @Override
            public void setOnItemClick(View view, int pos) {
                isOrder = false;
                post = pos;
                //场次时间只能选择一个
                jsonObject = null;
                jsonObject = new JSONObject();
                keyList.clear();
                checkSeat.clear();
                num_ticket.setText("0");
                planBean = randomList.get(pos);
//                if(randomList.get(pos).getState().equals("0")){//判断是否有票 0 无票 1 有票
//                    RxToast.info(mcontext.getResources().getString(R.string.price_null));
//                }
                getRandomList("" + randomList.get(pos).getGoodsId(), "" + randomList.get(pos).getId(), mcontext);
            }
        });
    }

    /**
     * 定义价格选择的列表（非在线选座）
     */
    private void initListPrice(Activity mcontext) {
        list_price.setLayoutManager(UIUtils.getRecyclerViewManager(true, mcontext));
        PriceListAdapter priceListAdapter = new PriceListAdapter(mcontext, timeData);
        list_price.setAdapter(priceListAdapter);
        priceListAdapter.OnItemClick(new PriceListAdapter.OnItemClick() {
            @Override
            public void setOnItemClick(View view, int pos) {
                //只能选择一种价格的
                jsonObject = null;
                jsonObject = new JSONObject();
                keyList.clear();
                checkSeat.clear();
                num_ticket.setText("0");
                isOrderGet = true;
                isOrder = true;
                seatData.clear();
                praiseSeatJson = timeData.get(pos);
                seatData.add(praiseSeatJson);
                initSeatList(mcontext);
            }
        });
    }

    /**
     * 定义座位选择的列表
     */
    private void initSeatList(Activity mcontext) {
        list_site.setLayoutManager(UIUtils.getRecyclerViewManager(true, mcontext));
        SeatListAdapter listAdapter = new SeatListAdapter(mcontext, seatData);
        list_site.setAdapter(listAdapter);
        listAdapter.OnItemClick(new SeatListAdapter.OnItemClick() {
            @Override
            public void setOnItemClick(View view, int pos) {
            }
        });
    }


    private String content = "";//输入框的信息

    /**
     * 显示输入评论信息的弹出框
     * item回复子项的点击
     */
    public void showCommentDialog(Activity activity, ReplyContent item, Comment commentBean) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_input_content, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);

        final EditText comment = view.findViewById(R.id.comment);
        final TextView noc = view.findViewById(R.id.noc);

        TextView cancel = view.findViewById(R.id.input_cancel);
        final Button ensure = (Button) view.findViewById(R.id.input_confirm);
        LinearLayout dia_log = (LinearLayout) view.findViewById(R.id.dia_log);

        if (item != null && commentBean != null) {
            String name = "@" + item.getUserName() + ":";
            String ht = "<font color='#108EE9'>" + name + "</font>" + activity.getResources().getString(R.string.input_hint);
            comment.setHint(Html.fromHtml(ht));
        } else if (commentBean != null && item == null) {
            String name = "@" + commentBean.getUserName() + ":";
            String ht = "<font color='#108EE9'>" + name + "</font>" + activity.getResources().getString(R.string.input_hint);
            comment.setHint(Html.fromHtml(ht));
        } else {
            comment.setHint(activity.getResources().getString(R.string.input_hint));
        }
        if (content != null && !content.equals("")) {
            comment.setText(content);
            comment.setSelection(content.length());
            noc.setText(content.length() + "/240");
            ensure.setTextColor(activity.getResources().getColor(R.color.main_color));
        }
        comment.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                noc.setText(comment.getText().toString().trim().length() + "/240");
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    ensure.setTextColor(activity.getResources().getColor(R.color.main_color));
                } else {
                    ensure.setTextColor(activity.getResources().getColor(R.color.text_b2));
                }
            }
        });
        ensure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //提交
                dialog.dismiss();
                if (comment.getText().toString().trim().length() < 10) {
//                    RxToast.info("评论内容请多于10个字");
                    ToastUtil.getToastEmail().ToastShow(activity, null, 1, R.layout.toast_view, activity.getResources().getString(R.string.opinion_msg));
                    comment.setText("");
                    content = "";
                    return;
                }
                InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
                }
                content = comment.getText().toString();
                Map<String, String> maps = new HashMap<>();
                if (item != null && commentBean != null) {//回复子评论
                    maps.put("type", "0");
                    maps.put("typeId", getId);
                    maps.put("content", content);
                    maps.put("mainCommentId", commentBean.getId() + "");
                    maps.put("replyType", "1");
                    maps.put("replyCommentId", item.getId() + "");
                    maps.put("replyUserId", item.getUserId() + "");
                    Api.getInstanceService(Constants.Ticket_URL).getAddReplyComment(maps).enqueue(new BaseCallback<BaseJSON>() {
                        @Override
                        public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                            //发布成功的信息互调
                            result.returnMessage = "发布成功";
                            comment.setText("");
                            content = "";
                            new Handler().postDelayed(new Runnable() {
                                public void run() {
                                    EventBus.getDefault().post(result);
                                }
                            }, 100);
                        }
                    });
                } else if (commentBean != null && item == null) {//回复主评论
                    maps.put("type", "0");
                    maps.put("typeId", getId);
                    maps.put("content", content);
                    maps.put("mainCommentId", commentBean.getId() + "");
                    maps.put("replyType", "0");
                    maps.put("replyCommentId", commentBean.getId() + "");
                    maps.put("replyUserId", commentBean.getUserId() + "");
                    Api.getInstanceService(Constants.Ticket_URL).getAddReplyComment(maps).enqueue(new BaseCallback<BaseJSON>() {
                        @Override
                        public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                            //发布成功的信息互调
                            result.returnMessage = "发布成功";
                            comment.setText("");
                            content = "";
                            new Handler().postDelayed(new Runnable() {
                                public void run() {
                                    EventBus.getDefault().post(result);
                                }
                            }, 100);
                        }
                    });
                } else {
                    maps.put("type", "0");
                    maps.put("typeId", "" + getId);
                    maps.put("content", content);
                    Api.getInstanceService(Constants.Ticket_URL).getaddComment(maps).enqueue(new BaseCallback<BaseJSON>() {
                        @Override
                        public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                            //发布成功的信息互调
                            result.returnMessage = "发布成功";
                            comment.setText("");
                            content = "";
                            new Handler().postDelayed(new Runnable() {
                                public void run() {
                                    EventBus.getDefault().post(result);
                                }
                            }, 100);
                        }
                    });
                }

            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //这里执行相应的删除代码
                dialog.dismiss();
                comment.setText("");
                content = "";
                InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
                }

            }
        });
        dialog.show();

        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        if (dialog.isShowing()) {//显示软键盘
            activity.getWindow().getDecorView().postDelayed(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        comment.requestFocus();
                        imm.showSoftInput(comment, 0);
                    }
                }
            }, 100);
        }
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                if (!comment.getText().toString().equals("")) {
                    content = comment.getText().toString();
                }
            }
        });

    }


    /**
     * 删除评论的操作
     *
     * @param activity
     */
    public void deleteComment(Activity activity, String id, int pos) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_delete_shaow, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        TextView textCancel = view.findViewById(R.id.btn_qd);
        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView textSure = view.findViewById(R.id.btn_success);
        textSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Api.getInstanceService(Constants.Ticket_URL).deleteComment(id).enqueue(new BaseCallback<BaseJSON>() {
                    @Override
                    public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                        //删除成功的操作
                        BaseJSON baseJSON = new BaseJSON();
                        baseJSON.returnMessage = "删除成功";
                        comments.remove(pos);
                        getView().setData(comments, false);
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                EventBus.getDefault().post(baseJSON);
                            }
                        }, 100);
                    }
                });

            }
        });
    }


    /**
     * 举报评论的操作
     *
     * @param activity
     */
    public void tipComment(Activity activity, Typeface iconfont, List<TipCommentBean> list, String id) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dilog_tip_show, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        TextView textCancel = view.findViewById(R.id.tip_btn_close);
        textCancel.setTypeface(iconfont);
        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        RecyclerView tip_list = view.findViewById(R.id.tip_list);
        tip_list.setLayoutManager(UIUtils.getRecyclerViewManager(false, activity));
        TipListAdapter tipListAdapter = new TipListAdapter(activity, list, iconfont);
        tip_list.setAdapter(tipListAdapter);
        tipListAdapter.OnItemClick(new TipListAdapter.OnItemClick() {
            @Override
            public void setOnItemClick(View view, int pos) {
                dialog.dismiss();
                Map<String, String> stringMap = new HashMap<>();
                stringMap.put("id", id);
                stringMap.put("content", list.get(pos).getValue());
                Api.getInstanceService(Constants.Ticket_URL).addTipComment(stringMap).enqueue(new BaseCallback<BaseJSON>() {
                    @Override
                    public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                        //举报成功的操作
                        BaseJSON baseJSON = new BaseJSON();
                        baseJSON.returnMessage = "举报成功";
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                EventBus.getDefault().post(baseJSON);
                            }
                        }, 100);
                    }
                });
            }
        });
    }

    /**
     * 评分的操作
     * @param activity
     */
    public void setStarComment(Activity activity, String id) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_starbar_show, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        StarBar starBar=view.findViewById(R.id.star_bar);
        TextView textCancel = view.findViewById(R.id.btn_qd);
        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView textSure = view.findViewById(R.id.btn_success);
        textSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Map<String,String> stringMap=new HashMap<>();
                stringMap.put("orderId",id);
                stringMap.put("star",""+starBar.getStarMark());
                stringMap.put("content","");
                Api.getInstanceService(Constants.Ticket_URL).setStarBarComment(stringMap).enqueue(new BaseCallback<BaseJSON>() {
                    @Override
                    public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                        //评分成功的操作
                        BaseJSON baseJSON = new BaseJSON();
                        baseJSON.returnMessage = "评分成功";
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                EventBus.getDefault().post(baseJSON);
                            }
                        }, 100);
                    }
                });

            }
        });
    }

    List<CardJsonBean> dataList = new ArrayList<>();//卡券列表数据
    /**
     * 选择优惠券列表
     * @param activity
     * @param goodId
     */
    public void getCouponList(Activity activity,String goodId,Typeface iconfont){
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_ticket_coupon, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = activity.getResources().getDisplayMetrics().heightPixels / 4 * 3;
        window.setAttributes(lp);
        RecyclerView recycler_list=view.findViewById(R.id.coupon_rv);
        recycler_list.setLayoutManager(UIUtils.getRecyclerViewManager(false,activity));
        CardReceiveListAdapter listAdapter=new CardReceiveListAdapter(activity,iconfont);
        recycler_list.setAdapter(listAdapter);
        dataList.clear();
        CardJsonBean cardJsonBean=new CardJsonBean();
        dataList.add(cardJsonBean);
        dataList.add(cardJsonBean);
        dataList.add(cardJsonBean);
        dataList.add(cardJsonBean);
        dataList.add(cardJsonBean);
        listAdapter.setData(dataList);
        Button sure_button=view.findViewById(R.id.sure_button);
        sure_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                ToastUtil.getToastEmail().ToastShow(activity, null, 1, R.layout.toast_view, activity.getResources().getString(R.string.coupon_complete));
            }
        });
    }
}
